package com.zx.pm.controller;

import com.zx.common.core.controller.BaseController;
import com.zx.common.core.domain.AjaxResult;
import com.zx.common.core.page.TableDataInfo;
import com.zx.common.utils.poi.ExcelUtil;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.multipart.MultipartFile;
import com.zx.pm.domain.Project;
import com.zx.pm.service.ProjectService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("pm/projects")
public class ProjectController extends BaseController {

    @Resource
    private ProjectService projectService;

    // 查询项目列表
    @GetMapping("list")
    public TableDataInfo list(Project project) {
        startPage();
        List<Project> list = projectService.selectList(project);
        return getDataTable(list);
    }

    // 查询所有项目(不分页),查询所有项目用于项目名称和合同号的联想查询
    @GetMapping("/getAll")
    public AjaxResult getAll(Project project){
        List<Project> list = projectService.selectList(project);
        return AjaxResult.success(list);
    }

    @PostMapping("add")
    public AjaxResult add(@Validated @RequestBody Project project) {
        //添加项目前先检查项目名称以及合同号是否存在
        if(projectService.checkProjectName(project)) {
            return error("项目已存在");
        }
        if(projectService.checkProjectNo(project)) {
            return error("合同号已经存在");
        }

        //添加创建者
        project.setCreateUserId(getUserId());
        project.setCreateTime(new Date());

        //添加项目
        Integer n = projectService.insert(project);

        return AjaxResult.success(n);
    }

    // 根据ID查询项目详情
    @GetMapping("{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        Project project = projectService.selectById(id);
        return AjaxResult.success(project);
    }

    @PutMapping("update")
    public AjaxResult update(@Validated @RequestBody Project project) {
        Integer n = projectService.update(project);
        return AjaxResult.success(n);
    }

    // 导出项目列表
    @PostMapping("export")
    public void export(HttpServletResponse response, Project project) {
        List<Project> list = projectService.selectList(project);
        ExcelUtil<Project> util = new ExcelUtil<>(Project.class);
        util.exportExcel(response, list, "项目信息");
    }

    // 导入项目数据
    @PostMapping("importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<Project> util = new ExcelUtil<>(Project.class);
        List<Project> dataList = util.importExcel(file.getInputStream());
        int success = 0;
        for (Project item : dataList) {
            if (updateSupport && item.getId() != null) {
                success += projectService.update(item);
            } else {
                success += projectService.insert(item);
            }
        }
        return AjaxResult.success("导入成功" + success + "条");
    }

    // 导入模板
    @PostMapping("importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<Project> util = new ExcelUtil<>(Project.class);
        util.importTemplateExcel(response, "项目信息");
    }

}
